IntFLASH
 
Component IntFLASH
Internal FLASH
Component Level: High
Typical Usage:
(Examples of a typical usage of the component in user code. For more information please see the page Component Code Typical Usage.)

In our examples we expect that there is an empty flash sector at 0xE000 address. It is also expected, that affected FLASH memory area is not protected. Please refer to CPU datasheet for details about protection. The user has to properly choose the address in the flash for writing the data and ensure that sector that will be over-written does not contain an application code or data, because the whole sector may be erased and lost. This can be done for example, by decreasing the size of the area where the code is stored by the size of one or more sectors that will be used for user's data. To adjust the allocation sizes, open the build options tab in CPU inspector.

Typical usage examples:

(1)
Simple read and write of internal FLASH memory.

Required component settings:

  • Component name: IntF1
  • Write mode: following options can be used
    • Safe Write - will overwrite only specified location, sector will be automatically erased and data restored (EraseSector method is not necessary)
    • Destructive Write - whole sector will be automatically erased before write (EraseSector method is not necessary)
    • Write - it is necessare to use EraseSector method before write or ensure the location is erased before write
 MAIN.C

byte Data;

void main(void)
{
  /* Erase whole sector - necessary only if Write mode property is set to "Write" */
  IFsh1_EraseSector(0xE000);
  /* Write number 28 to address 0xE000 */
  IntF1_SetByteFlash(0xE000, 28);
  /* Read contents of internal FLASH array on address
     0xE000 and write it to variable Data */
  IntF1_GetByteFlash(0xE000, &Data);

}
Note: Address of FLASH is meant as absolute address, not index in FLASH array.

(2)
If 'Virtual page' property is enabled, methods using virtual page are available. The following example reads data content of some FLASH area, modifies it, and stores it in other place in FLASH.

Required component settings:

  • Component name: IntF1
  • Write mode: Safe Write
  • Virtual page: Enabled
 MAIN.C


void main(void)
{

  // Fill virtual page with actual data content of address 0xE000
  IntF1_GetPage(0xE000);
  // Write number 23 to first cell of page
  IntF1_SetBytePage(0, 23);
  // Write number 255 to second cell
  IntF1_SetBytePage(1, 255);
  // Store data content of virtual page to FLASH on address 0xE630
  IntF1_SetPage(0xE630);

}
Note: Method SetBytePage/GetBytePage use index 0 to PageSize-1

(3)
If Write method property is set to Safe write and user buffer is selected in Buffer type property, it is necessary to implement events OnSaveBuffer and OnRestoreBuffer. These events are called if flash sector erase operation is necessary for writing data into FLASH. It is expected, that the user code saves/restores sector content during sector-erase operation.

The chart below shows a sequence of program flow during write opration if sector erase operation is required:

In the following example, sector content is copied into RAM LocBuffer variable in OnSaveBuffer event and it is restored in OnRestoreBuffer event.
Main contains "dummy" code, that causes sector erase.

Required component settings:

  • Component name: IntF1
  • Write mode: Safe Write
  • Buffer type: Implemennted by user
 EVENTS.C

#define MASK_ADDR_OFFSET (IntF1_AREA_0_SECTOR_SIZE - 1)
static byte LocBuffer[IntF1_AREA_0_SECTOR_SIZE];

void IntF1_OnSaveBuffer(IntF1_TAddress Addr, word Size)
{
  byte * Buf = &LocBuffer[Addr & MASK_ADDR_OFFSET];
  while (Size--) {
    (void)IntF1_GetByteFlash(Addr, Buf);
    Addr++;
    Buf++;
  } /* while Size */
}

void IntF1_OnRestoreBuffer(IntF1_TAddress Addr, word Size)
{
  (void)IntF1_RestoreToFlash(Addr, &LocBuffer[Addr & MASK_ADDR_OFFSET], Size);
}


 MAIN.C

void main(void)
{
  IntF1_SetByteFlash(0xE000, 0); /* clear */
  IntF1_SetByteFlash(0xE000, 1); /* sector erase operation will be required */
}

(4)
DataPtr2Addr and FuncPtr2Addr method can be used to convert pointer to data respective pointer to function to an address, that may be passed as a parameter to write methods (SetByteFlash, SetWordFlash, etc.). Example will demonstrate how to rewrite constant data in the Flash memory.

Required component settings:

  • Component name: IntF1
  • Write mode: Safe Write
 MAIN.C

const byte tab []@0xE000={
  1,2,3,4,5
};


void main(void)
{

  // rewrite first byte of constant variable tab
  IFsh1_SetByteFlash(IFsh1_DataPtr2Addr(tab),55);

}